<template>
  <div class="cart-control">
    <transition name="move">
      <div class="cart-decrease " v-show="foodNum > 0" @click.stop.prevent="decreaseCart($event, food)">
        <span class="inner icon-remove_circle_outline" :class="{'disable-click': disableDec}"></span>
      </div>
    </transition>
    <div class="cart-count" v-show="foodNum > 0">{{foodNum}}</div>
    <div class="cart-add icon-add_circle" @click.stop.prevent="addCart($event, food)"></div>
  </div>
</template>

<script>
  export default {
    props: {
      food: {
        type: Object
      },
      cartFoods: {
        type: Array
      }
    },
    methods: {
      addCart(event, food) {
        // console.log(event)
        // console.log(food)
        //  阻止非Vue事件
        if (!event._constructed) {
          return
        }
        this.$emit('add', food, event.target)
      },
      decreaseCart(event, food) {
        if (!event._constructed) {
          return
        }
        this.$emit('dec', food, this.foodNum, this.disableDec)
      }
    },
    computed: {
      foodNum() {
        let num = 0
        if (this.cartFoods) {
          this.cartFoods.filter(({ id }) => id === this.food.id).forEach(({ acount: account }) => {
            num += account
          })
          return num
        } else {
          return this.food.acount || 0
        }
      },
      disableDec() {
        if (!this.cartFoods) {
          return
        }
        const foodInCart = this.cartFoods.filter(({ id }) => id === this.food.id)
        return foodInCart.length > 1
      }
    }
  }
</script>

<style lang="stylus" rel="stylesheet/stylus">
  .cart-control
      font-size: 0
    .cart-decrease
      display: inline-block
      padding: 6px
      transition: all .4s linear
      &.move-enter-active
        opacity: 1
        transform: translate3d(0, 0, 0)
      .inner
        display: inline-block
        line-height: 24px
        font-size: 24px
        color: rgb(0, 160, 220)
        transition: all .4s linear
        transform: rotate(0)
        &.disable-click
          color: #cbced2
      &.move-enter, &.move-leave-to
        opacity: 0
        transform: translate3d(24px, 0, 0)
        .inner
          transform: rotate(180deg)
    .cart-count
      display: inline-block
      vertical-align: top
      padding-top: 6px
      line-height: 24px
      text-align: center
      font-size: 10px
      color: rgb(147, 153, 159)
    .cart-add
      display: inline-block
      padding: 6px
      line-height: 24px
      font-size: 24px
      color: rgb(0, 160, 220)
</style>
